from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QFrame, QLabel, QHBoxLayout, QPushButton
from func import view
from config import Text_config
from func import thred
from comment_ import Comment
from chat_user import Chat_User

class OrderUpdateSignal(QObject):
    data_updated = pyqtSignal(dict)


class His_order(QDialog):
    def __init__(self,parent=None):
        super().__init__()
        self.parent=parent
        self.user_id=self.parent.user_id
        self.user_name=self.parent.user_name
        self.client=self.parent.client
        self.setWindowTitle('历史订单')
        self.resize(800,400)
        self.order_signal = OrderUpdateSignal()
        self.order_signal.data_updated.connect(self.update_order_display)

        mainLayout = QVBoxLayout(self)
        self.initUI(mainLayout)
    def initUI(self, mainLayout):
        parser={
            'user_id':self.user_id,
        }
        data1=self.client.get_orders(parser)
        self.no_finish_order = data1.get('no_finish_order')
        self.finish_order = data1.get('finish_order')
        self.stop_event = thred.star_thread(thred.seller_request_client, self.client, parser, data1,self.order_signal)
        frame=view.create_scroll(mainLayout)
        btn_exit=QPushButton()
        btn_exit.setText('返回')
        btn_exit.clicked.connect(self.exit)
        mainLayout.addWidget(btn_exit)
        layout1=QVBoxLayout(frame)
        self.frame_order=QFrame()
        layout1.addWidget(self.frame_order)
        self.create_order()

    def create_order(self):
        view.clear_layout(self.frame_order.layout())
        layout_order=self.frame_order.layout()
        if layout_order ==None:
            layout_order = QVBoxLayout(self.frame_order)
        label = QLabel()
        layout_order.addWidget(label)
        label.setText('未完成的订单')
        label.setStyleSheet(Text_config.text_style2)
        for i in range(len(self.no_finish_order)):
            layout=QHBoxLayout()
            layout_order.addLayout(layout)
            label=QLabel()
            text=f'日期：{self.no_finish_order[i][0]}'
            label.setText(text)
            layout.addWidget(label)
            label=QLabel()
            text=f'商品名：{self.no_finish_order[i][1]}'
            label.setText(text)
            layout.addWidget(label)
            label=QLabel()
            text=f'商品数量：{self.no_finish_order[i][2]}'
            label.setText(text)
            layout.addWidget(label)
            label=QLabel()
            text=f'商家：{self.no_finish_order[i][3]}'
            label.setText(text)
            layout.addWidget(label)
            btn_chat=QPushButton()
            btn_chat.setText('联系商家')
            btn_chat.setProperty('seller_name',self.no_finish_order[i][3])
            layout.addWidget(btn_chat)
            btn_delete=QPushButton()
            btn_delete.setText('取消订单')
            btn_delete.setProperty('order_id',self.no_finish_order[i][4])
            layout.addWidget(btn_delete)
            btn_chat.clicked.connect(self.chat_seller)
            btn_delete.clicked.connect(self.delete_orders)
        label = QLabel()
        layout_order.addWidget(label)
        label.setText('以完成的订单')
        label.setStyleSheet(Text_config.text_style2)
        for i in range(len(self.finish_order)):
            layout = QHBoxLayout()
            layout_order.addLayout(layout)
            label = QLabel()
            text = f'日期：{self.finish_order[i][0]}'
            label.setText(text)
            layout.addWidget(label)
            label = QLabel()
            text = f'商品名：{self.finish_order[i][1]}'
            label.setText(text)
            layout.addWidget(label)
            label = QLabel()
            text = f'商品数量：{self.finish_order[i][2]}'
            label.setText(text)
            layout.addWidget(label)
            label = QLabel()
            text = f'商家：{self.finish_order[i][3]}'
            label.setText(text)
            layout.addWidget(label)
            btn_comment = QPushButton()
            if self.finish_order[i][5] == 0:
                btn_comment.setText('评论')
                btn_comment.setFixedWidth(50)
                btn_comment.clicked.connect(self.comment)
            else:
                dic={
                    1 : '🤬🤬答辩玩意',
                    2 : '😠😠不愿多说',
                    3 : '😑😑马马虎虎',
                    4 : '😗😗还不错哟',
                    5 : '😍😍十分喜欢'
                }
                btn_comment.setText('查看评论')
                text=f'我的评价：{dic[self.finish_order[i][5]]}'
                btn_comment.setToolTip(text)
                btn_comment.setEnabled(False)
            btn_comment.setFixedWidth(80)
            btn_comment.setProperty('order_id', self.finish_order[i][4])
            btn_comment.setProperty('shop_name', self.finish_order[i][1])
            layout.addWidget(btn_comment)
            btn_chat = QPushButton()
            btn_chat.setText('联系商家')
            btn_chat.setProperty('seller_name', self.finish_order[i][3])
            layout.addWidget(btn_chat)
            btn_chat.clicked.connect(self.chat_seller)

    def update_order_display(self,data):
        print('数据发送改变')
        self.no_finish_order = data.get('no_finish_order')
        self.finish_order = data.get('finish_order')
        self.create_order()


    def comment(self):
        sender = self.sender()
        self.statu=True
        self.order_id=sender.property('order_id')
        self.shop_name=sender.property('shop_name')
        print(self.order_id)
        com=Comment(self)
        res =com.exec_()
    def chat_seller(self):
        sender = self.sender()
        seller_name = sender.property('seller_name')
        self.recv_name=seller_name
        parser={
            'user_name' : self.recv_name,
        }
        self.recv_id=self.client.get_user(parser).get('data').get('user_id')
        my = Chat_User(self)
        my.exec_()


    def delete_orders(self):
        sender = self.sender()
        order_id = sender.property('order_id')
        parser={
            'orders_id':order_id,
        }
        msg=self.client.delete_orders(parser)
        return msg

    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()

    def closeEvent(self,event):
        if self.stop_event is not None:
            self.stop_event.set()
            self.stop_event = None




